﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_Class;

namespace NLLSite.Administrator.UserControl
{
    public partial class ManageEbook : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel2.Visible = true;
            CheckBox1.Visible = false;
            btnRemove.Visible = false;
            Button1.Visible = false;

            Session["IDebook"] = GridView1.DataKeys[GridView1.SelectedIndex].Value;
            txtName.Text = HttpUtility.HtmlDecode(GridView1.SelectedRow.Cells[2].Text);
            txtImage.Text = HttpUtility.HtmlDecode(((Label)GridView1.SelectedRow.FindControl("Label1")).Text);
            CKEditor1.Text = HttpUtility.HtmlDecode(((Label)GridView1.SelectedRow.FindControl("Label2")).Text);
            txtFile.Text = HttpUtility.HtmlDecode(((Label)GridView1.SelectedRow.FindControl("Label3")).Text);
        }

        protected void txtCancel_Click(object sender, EventArgs e)
        {
            Panel2.Visible = false;
            CheckBox1.Visible = true;
            btnRemove.Visible = true;
            Button1.Visible = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SachDT s = new SachDT();
            Ebook eb = new Ebook();

            eb.ID = Int32.Parse(Session["IDebook"].ToString());
            eb.Title = txtName.Text;
            eb.ImageUrl = txtImage.Text;
            eb.Description = CKEditor1.Text;
            eb.FileUrl = txtFile.Text;

            s.UpdateEbook(eb);

            Panel2.Visible = false;
            CheckBox1.Visible = true;
            btnRemove.Visible = true;
            GridView1.DataBind();
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("cbxDelete");
                if (CheckBox1.Checked)
                {
                    cb.Checked = true;
                }
                else
                {
                    cb.Checked = false;
                }
            }
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            SachDT db = new SachDT();

            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("cbxDelete");
                if (cb != null && cb.Checked)
                {
                    db.DeleteEbook(Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value));
                }
            }
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemEbook.aspx");
        }
    }
}